import type { Moment } from 'moment';
import moment from 'moment';
import type { SensorTime } from '../data';
import type { DeviceStatusBox, DeviceStatusItem } from '../List/data';
import type { ProSchemaValueEnumType } from '@ant-design/pro-utils/lib/typing';

export const DeviceStatusMap = [
  {
    enum: [-1],
    key: 'unknown',
    label: '未知',
    color: 'purple',
  },
  {
    enum: [0, 2],
    key: 'lock_free',
    label: '无车 上锁',
    color: 'green',
  },
  {
    enum: [1, 3],
    key: 'lock_occupied',
    label: '有车 上锁',
    color: 'red',
  },
  {
    enum: [4, 6],
    key: 'unlock_free',
    label: '无车 落锁',
    color: 'green',
  },
  {
    enum: [5, 7],
    key: 'unlock_occupied',
    label: '有车 落锁',
    color: 'red',
  },
];

export const serviceStatusMapping: (ProSchemaValueEnumType & { key: number; option: boolean })[] = [
  {
    key: -1,
    text: '全部',
    color: 'blue',
    status: 'normal',
    option: false,
  },
  {
    key: 0,
    text: '未知',
    color: 'grey',
    status: 'disabled',
    option: false,
  },
  {
    key: 1,
    text: '服务正常',
    color: 'green',
    status: 'success',
    option: true,
  },
  {
    key: 2,
    text: '服务下线',
    color: 'yellow',
    status: 'exception',
    option: true,
  },
  {
    key: 10,
    text: '电量低',
    color: 'blue',
    status: 'exception',
    option: true,
  },
  {
    key: 11,
    text: '设备故障',
    color: 'red',
    status: 'exception',
    option: true,
  },
];

export const deviceTypeParse = {
  parse: (name: string) => {
    if (name === 'lock') {
      return '车位锁';
    }
    if (name === 'sensor') {
      return '检测器';
    }
    return '未知';
  },
};

export const DeviceStatusParser = {
  parser: (statusBox: DeviceStatusBox | undefined) => {
    const status: DeviceStatusItem[] = [];
    if (statusBox === undefined) {
      return status;
    }
    if (statusBox.hasCar) {
      status.push({ color: 'blue', label: '有车', key: '有车' });
    } else {
      status.push({ color: 'green', label: '无车', key: '无车' });
    }
    if (statusBox.armDown) {
      status.push({ color: 'blue', label: '开锁', key: '开锁' });
    } else {
      status.push({ color: 'green', label: '上锁', key: '上锁' });
    }
    if (statusBox.armWarning) {
      status.push({ color: 'red', label: '报警', key: '报警' });
    }
    return status;
  },
};

export const xstatus = {
  parse: (num: number) => {
    const obj =
      DeviceStatusMap.find((item) => num && item.enum?.includes(num)) || DeviceStatusMap[0];
    return { ...obj, val: num };
  },
  parses: (statusBox: DeviceStatusBox | undefined) => DeviceStatusParser.parser(statusBox),
};

export const serviceStatusParse = {
  parse: (num: number) => {
    const obj =
      serviceStatusMapping.find((item) => item.key === num) ||
      serviceStatusMapping.find((o) => o.key === -1);
    if (obj != null && num <= 0) {
      return { ...obj, val: num, text: '未知' };
    }
    return { ...obj, val: num };
  },
};
export const xtime = (time?: number | Date | Moment): SensorTime => {
  if (!time) return { val: -1, label: '0000-00-00 00:00:00', color: 'purple' };
  const t = moment(time);
  return {
    val: t.unix(),
    label: t.format('YYYY-MM-DD HH:mm:ss'),
    color: t.unix() < Date.now() - 4 * 3600000 ? 'red' : 'green',
  };
};
